package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection con;
    private PreparedStatement pre;
    @Override
    public List<Business> listBusinessByOrderTypeId(int orderTypeId) throws SQLException {
        List<Business> listBusiness = new ArrayList<Business>();
        String sql = "SELECT * FROM business WHERE orderTypeId=? ORDER BY businessId";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            pre.setInt(1, orderTypeId);
            ResultSet rs = pre.executeQuery(); //执行查询
            while (rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                listBusiness.add(business);
            }
        }  finally {
            DBUtil.close(pre); //关闭资源
        }
        return listBusiness;
    }

    @Override
    public Business getBusinessById(int businessId) throws SQLException {
        Business business  = null;
        String sql = "SELECT * FROM business WHERE businessId=?";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            pre.setInt(1, businessId);
            ResultSet rs = pre.executeQuery(); //执行查询
            while (rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }  finally {
            DBUtil.close(pre); //关闭资源
        }
        return business;
    }
}
